package com.leoman.service.impl;

import com.leoman.core.Constant;
import com.leoman.dao.AreaDao;
import com.leoman.entity.Area;
import com.leoman.service.AreaService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/3/8.
 */
@Service
public class AreaServiceImpl implements AreaService {

    @Autowired
    private AreaDao areaDao;

    @Override
    public List<Area> findAll() {
        return areaDao.findList();
    }

    @Override
    public Page<Area> find(Integer pageNum, Integer pageSize) {
        return areaDao.findList(new PageRequest(pageNum - 1, pageSize));
    }

    @Override
    public Page<Area> find(Integer pageNum) {
        return areaDao.findList(new PageRequest(pageNum - 1, Constant.PAGE_DEF_SIZE));
    }

    @Override
    public Area getById(Integer id) {
        return areaDao.findOne(id);
    }

    @Override
    public Area deleteById(Integer id) {
        Area area = areaDao.findOne(id);
        area.setIsCut(Constant.IS_CUT_YES);
        areaDao.save(area);
        return null;
    }

    @Override
    public Area create(Area area) {
        return areaDao.save(area);
    }

    @Override
    public Area update(Area area) {
        return areaDao.save(area);
    }

    @Override
    @Transactional
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            deleteById(id);
        }
    }

    @Override
    public Page<Area> page(final Integer parentId, final Integer level, final String name, Integer pageNum, Integer pageSize) {
        PageRequest pageRequest = new PageRequest(pageNum - 1, pageSize, Sort.Direction.ASC, "id");

        return areaDao.findAll(new Specification<Area>() {
            @Override
            public Predicate toPredicate(Root<Area> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate result = null;
                List<Predicate> predicateList = new ArrayList<>();
                if (null != parentId) {
                    Predicate pre = cb.equal(root.get("parentId").as(Integer.class), parentId);
                    predicateList.add(pre);
                }
                if (null != level) {
                    Predicate pre = cb.equal(root.get("level").as(Integer.class), level);
                    predicateList.add(pre);
                }
                if (StringUtils.isNotBlank(name)) {
                    Predicate pre = cb.like(root.get("name").as(String.class), "%" + name + "%");
                    predicateList.add(pre);
                }

                Predicate pre1 = cb.equal(root.get("isCut").as(Integer.class), Constant.IS_CUT_NO);
                predicateList.add(pre1);

                if (predicateList.size() > 0) {
                    result = cb.and(predicateList.toArray(new Predicate[]{}));
                }

                if (result != null) {
                    query.where(result);
                }
                return query.getGroupRestriction();
            }

        }, pageRequest);
    }

    @Override
    public List<Area> findParentList() {
        return areaDao.findListByParams();
    }

    @Override
    public List<Area> findList(Integer areaId) {
        return areaDao.findListByParentId(areaId);
    }
}
